package org.bsc.mgwt.client.mvp;

import org.bsc.mgwt.client.ClientFactory;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.SimplePanel;
import com.googlecode.mgwt.mvp.client.AnimatableDisplay;
import com.googlecode.mgwt.mvp.client.AnimatingActivityManager;
import com.googlecode.mgwt.mvp.client.AnimationMapper;
import com.googlecode.mgwt.mvp.client.MGWTAbstractActivity;
import com.googlecode.mgwt.mvp.client.display.AnimatableDisplayWithoutAnimation;

/**
 * 
 * @author softphone
 *
 */
public abstract class MVPAbstractActivity extends MGWTAbstractActivity  {

		public static final Activity EMPTY_ACTIVITY = new AbstractActivity() {
			
			SimplePanel emptyPanel = new SimplePanel();
			
			@Override
			public void start(AcceptsOneWidget panel, com.google.gwt.event.shared.EventBus eventBus) {
				panel.setWidget(emptyPanel);

			}
		};

		public static final Activity NULL_ACTIVITY = new AbstractActivity() {
			@Override
			public void start(AcceptsOneWidget panel, com.google.gwt.event.shared.EventBus eventBus) {
				panel.setWidget(null);

			}
		};

		//final PlaceHistoryHandler historyHandler;
		final PlaceController placeController;

		protected final AnimatingActivityManager manager ;
		
		/**
		 * Used to obtain views, eventBus, placeController.
		 * Alternatively, could be injected via GIN.
		 */
		protected final ClientFactory clientFactory;
		
		/**
		 * 
		 * @param clientFactory
		 */
		public MVPAbstractActivity(ClientFactory clientFactory, HasAnimatableDisplay view) {
			this.clientFactory = clientFactory;

			com.google.web.bindery.event.shared.EventBus eventBus = 
					new com.google.web.bindery.event.shared.SimpleEventBus();

			placeController = new PlaceController( eventBus );
			
			//DynamicPlaceHistoryMapper historyMapper = new DynamicPlaceHistoryMapper();
			
			//historyHandler = new PlaceHistoryHandler(historyMapper);

			//historyHandler.register(placeController, eventBus, new Tab2Place("") );
			
			
			final ActivityMapper activityMapperProxy = new ActivityMapper() {

				final ActivityMapper activityMapper = getActivityMapper();

				@Override
				public Activity getActivity(Place place) {
					
					Activity activity = activityMapper.getActivity(place);
				
					return (null!=activity) ? activity : EMPTY_ACTIVITY;
				}
				
			};
					
			manager = new  AnimatingActivityManager( activityMapperProxy, getAnimationMapper(), eventBus);
			manager.setFireAnimationEvents(false);
			
			manager.setDisplay( view.getDisplay() );
			
 			
		}

		public abstract AnimationMapper getAnimationMapper();
		
		public abstract ActivityMapper getActivityMapper();

		/**
		 * 
		 * @param place
		 */
		public final void goTo(Place place) {
			
			// TODO if is internal use private place controller otherwise use default
			
			//if( !isInternalPlace() ) 
			//	clientFactory.getPlaceController().goTo(place);
			//else
				placeController.goTo(place);
			
		}

		/**
		 * Need for fix bug related to first goto
		 * 
		 * @param place
		 */
		protected void goTo( final Place place, int delayMills ) {

			(new Timer() {

					@Override
					public void run() {
						goTo( place );	
					}
					
				}).schedule(delayMills);
			
		}

}
